package com.qfedu.edu.finishorderservice.queue.receive;

import com.alibaba.fastjson.JSON;
import com.qfedu.edu.finishorderservice.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author xiaobobo
 * @title: OrderQueueListener
 * @projectName seckill-fy-2401-parent
 * @description: 这个主要用来接受队列消息
 * @date 2024/11/12  9:29
 */
@Component
@Slf4j
public class OrderQueueListener {


    @Autowired
    private OrderService orderService;

    /**
     * 接受队列消息的地方
     * <p>
     * 业务代码是直接写到 下面呢 ？还是写到 Service中?
     * 这里的代码应该写到Service中 要保证事务一致性
     *
     * @param data
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {"orderQueue"})
    public void receive(byte[] data, Message message, Channel channel) throws IOException {
     //先将上面的数据弄成对象
     String receiveMessage = new String(data, "UTF-8");
     log.info("接收到的消息是:"+receiveMessage);
     orderService.createOrder(receiveMessage);
     channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }

}
